﻿Public Class frmVendors
    Private Sub frmVendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label3.Text = "ชื่อ"
        Me.lblTitle.Text = Me.Text
        Load_Data()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            Dim Strsql As String
            Dim Mysql1 As New ClsSQLhelper
            Dim DS As New DataSet
            If Me.Txtid.Text = "" Then
                Strsql = "Select * from vendor where vendorname= '" & Me.TxtName.Text & "'"
                DS = Mysql1.GetMYSQLDataset(Strsql, "DS_vendor", "vendor")
                If DS.Tables(0).Rows.Count <> 0 Then
                    MsgBox("Vendor นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                Strsql = "Insert into vendor (unitofmeasure,note) values ('" & Me.TxtName.Text & "', '" & Me.TxtNote.Text & "')"
                Debug.Print(Strsql)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            Else

                Strsql = "update vendor set vendorname= '" & Me.TxtName.Text & "', note = '" & Me.TxtNote.Text & "' "
                Strsql = Strsql & " where vendorid = " & Me.Txtid.Text.Trim
                Debug.Print(Strsql)
                Mysql1.MySQLExecute(Strsql)
                MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
            'Refresh Data
            Load_Data()
            New_Entry()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub New_Entry()
        Me.Txtid.Text = ""
        Me.TxtName.Text = ""
        Me.TxtNote.Text = ""
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub

            Dim Strsql As String
            Dim Mysql1 As New ClsSQLhelper
            Strsql = "Delete from vendor where vendorid = " & Me.Txtid.Text
            Debug.Print(Strsql)
            Mysql1.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub Load_Data()
        Dim Strsql As String
        Dim DS As New DataSet
        Dim Mysql1 As New ClsSQLhelper
        Strsql = "select vendorid,vendorname,note from vendor order by vendorname"
        DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Vendor", "vendor")
        Debug.Print(Strsql)
        With Me.griddetail
            .ReadOnly = True
            .RowHeadersVisible = False
            .AllowDrop = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = True
            .AllowUserToResizeRows = False
            .MultiSelect = False
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
            .DataSource = DS.Tables(0)
            '.DataMember = "jobcom_printer"
            DS.Dispose()

        End With
    End Sub

    Private Sub griddetail_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griddetail.CellClick
        With Me.griddetail
            If .RowCount = 0 Then Exit Sub
            Me.Txtid.Text = .Item(0, e.RowIndex).Value.ToString
            Me.TxtName.Text = .Item(1, e.RowIndex).Value.ToString
            Me.TxtNote.Text = .Item(2, e.RowIndex).Value.ToString
        End With

    End Sub

    Private Sub griddetail_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles griddetail.CellContentClick

    End Sub
End Class
